/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.ohos.speechutilsdemo.slice;

import com.ohos.speechutilsdemo.ResourceTable;

import ee.ioc.phon.ohos.speechutils.abilityslice.TtsBaseAbilitySlice;

import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.Text;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;

/**
 * 文字转语音
 *
 * @since 2021-03-20
 */
public class TtsAbilitySlice extends TtsBaseAbilitySlice {
    private static final HiLogLabel LABEL_LOG = new HiLogLabel(3, 0xD001100, "TTSAbilitySlice");
    /**
     * 需要转换为语音的文字
     */
    private Text textPhonetic;
    /**
     * 开始转换按钮
     */
    private Button startPlay;
    /**
     * 开始播放按钮
     */
    private Button stopPlay;
    /**
     * 转换耗时
     */
    private Text textTimes;

    @Override
    public int setLayout() {
        return ResourceTable.Layout_ability_tts;
    }

    @Override
    public void initView() {
        textPhonetic = (Text) findComponentById(ResourceTable.Id_text_phonetic);
        textTimes = (Text) findComponentById(ResourceTable.Id_text_times);
        startPlay = (Button) findComponentById(ResourceTable.Id_bt_tts_play);
        stopPlay = (Button) findComponentById(ResourceTable.Id_bt_tts_stop);
        startPlay.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                if (isInitItsResult) {
                    startTts(textPhonetic.getText());
                } else {
                    HiLog.error(LABEL_LOG, "initItsResult is false");
                }
            }
        });
        stopPlay.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                if (isSpeaking()) {
                    stopSpeaking();
                } else {
                    HiLog.error(LABEL_LOG, "已经播报完毕或者tts客户端初始化未成功！");
                }
            }
        });
    }

    /**
     * 耗费时间
     *
     * @param info 耗费时间
     */
    @Override
    public void showInfo(String info) {
        textTimes.setText(info);
    }
}
